package com.letoken.platform.app.service.impl;

import com.letoken.platform.pub.enums.LetokenApiResponseEnum;
import com.letoken.platform.pub.enums.TokenApprovedStateEnum;
import com.letoken.platform.pub.enums.TokenTransactionStatusEnum;
import com.letoken.platform.pub.exception.ApiServiceException;
import com.letoken.platform.pub.util.UUIDUtil;
import com.letoken.platform.repo.admin.dao.WithdrawMapper;
import com.letoken.platform.repo.admin.po.Withdraw;
import com.letoken.platform.repo.app.dao.AppUserIssueMapper;
import com.letoken.platform.repo.app.dao.AppUserRechargeMapper;
import com.letoken.platform.repo.app.po.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author wyl
 * @date 2023/2/9 16:40
 * @Version 1.0
 */
@Service
public class WithdrawCheckByUserIdServiceImpl {

    @Autowired
    private AppUserRechargeMapper appUserRechargeMapper;

    @Autowired
    private AppUserIssueMapper appUserIssueMapper;

    @Autowired
    private WithdrawMapper withdrawMapper;

    /**
     * 1、提现钱包，有无充值或铸币记录。若无，第二步
     * 2、查询U的站内收款记录的来源用户，看是否有充值或铸币记录，如果无，拒绝提现（提示：提现异常，请联系客服）
     * 3、判断提现金，大于1000U走审核流程，否则直接通过(is_auto=1)
     * 4、如果失败，插入一条失败的提现记录（备注原因：U来源异常，拒绝提现）
     *
     * @param userId                用户id
     * @param leUserAddressOrUserId 链上地址
     * @param amount                提现金额
     * @param token                 币种
     */
    public Withdraw withdrawCheckByUserId(Integer userId, String leUserAddressOrUserId, BigDecimal amount, Token token, String uuid) {
        boolean flag = false;
        //是否有充值记录
        Integer rechargeCount = appUserRechargeMapper.countByUserId(userId);
        if (rechargeCount < 1) {
            //是否有铸币记录
            Integer issueCount = appUserIssueMapper.countByUserId(userId);
            if (issueCount < 1) {
                //查询U的站内收款记录的来源用户（去重）
                List<Integer> userIdList = withdrawMapper.queryUserBytoUser(userId);
                if (userIdList.size() == 0) {
                    flag = true;
                } else {
                    //是否有充值记录
                    Integer rechargeListCount = appUserRechargeMapper.countByUserIdList(userIdList);
                    if (rechargeListCount < 1) {
                        //是否有铸币记录
                        Integer issueListCount = appUserIssueMapper.countByUserIdList(userIdList);
                        if (issueListCount < 1) {
                            flag = true;
                        }
                    }
                }
            }
        }
        if (flag) {
            Withdraw withdraw = new Withdraw();
            withdraw.setSn(uuid);
            withdraw.setUserId(userId);
            withdraw.setTokenId(token.getId());
            withdraw.setChainId(token.getChainId());
            withdraw.setAddress(leUserAddressOrUserId);
            withdraw.setAmount(amount);
            withdraw.setFeeAmount(BigDecimal.ZERO);
            withdraw.setTransferMode(1);
            withdraw.setTxid("");
            withdraw.setStatus(TokenTransactionStatusEnum.ERROR.getValue());
            withdraw.setResponse("U来源异常，拒绝提现");
            withdraw.setRemark("");
            withdraw.setCreateTime(LocalDateTime.now());
            withdraw.setApprovedBy("");
            withdraw.setApprovedState(TokenApprovedStateEnum.REFUSE.getValue());
            return withdraw;
        }
        return null;
    }
}
